package jtmcraft.game;

import java.awt.Color;
import jtmcraft.game.units.GameUnit;
import jtmcraft.game.units.GameUnitFactory;
import jtmcraft.game.units.GameUnitFactory.GameUnitType;
import jtmcraft.game.units.states.DefendState;

/**
 *
 * @author milosz
 */
public class GameController extends Controller
{
    private GameUnitFactory gameUnitFactory;

    public GameController() 
    {
        this.player = new Player();
        this.gameUnitFactory = new GameUnitFactory( this );
    }

    @Override
    public void addResource(int resource)
    {
        player.addResources( resource );
    }

    @Override
    public void attack(GameUnit attacker, GameUnit defender, double radian, double i) {
        if( null == defender.getState() || defender.getState().getClass() != DefendState.class ) {
            defender.setState( new DefendState( attacker, radian, i ) );
            super.attack(attacker, defender, radian, i);
        }
    }

    @Override
    public void destroyUnit(GameUnit unit) {        
        if( !player.removeUnit( unit ) ) {
            for( Player e : enemies ) {
                if( e.removeUnit( unit ) ) break;
            }
        }
        super.destroyUnit( unit );
    }

    @Override
    public void hit(GameUnit attacker, GameUnit defender, int power) {
        defender.decreseDurability( power );
        super.hit( attacker, defender, power );
    }

    @Override
    public void createBuildingUnit(GameUnitType type, int posX, int posY)
    {
        GameUnit unit = gameUnitFactory.createGameUnit( type, Color.GREEN  );
        unit.setX( posX );
        unit.setY( posY );
        if( type == GameUnitType.storehouse ) {
            player.setStorehouse( unit );
        } else {
            player.addBuildingUnit( unit );
        }
        super.createBuildingUnit( type, posX, posY );
    }

    @Override
    public void createMobileUnit(GameUnitType type, int posX, int posY)
    {
        GameUnit unit = gameUnitFactory.createGameUnit( type, player.getStorehouse(), Color.GREEN );
        unit.setX( posX );
        unit.setY( posY );
        player.addMobileUnit( unit );
        super.createBuildingUnit( type, posX, posY );
    }

    @Override
    public void createNeutralGameUnit(GameUnitType type, int posX, int posY) 
    {
        GameUnit unit = gameUnitFactory.createGameUnit( type );
        unit.setX( posX );
        unit.setY( posY );
        this.neutralGameUnits.add( unit );
        super.createNeutralGameUnit( type, posX, posY );
    }

    @Override
    public void createEnemyBuildingUnit( GameUnitType type, int posX, int posY, int enemyID )
    {
        if( null == enemies.get( enemyID ) ) {
            throw new IllegalStateException( "Synchronization failed" );
        }
        GameUnit unit = gameUnitFactory.createGameUnit( type, Color.RED );
        unit.setX( posX );
        unit.setY( posY );
        enemies.get( enemyID ).addBuildingUnit( unit );
        super.createEnemyBuildingUnit( type, posX, posY, enemyID );
    }

    @Override
    public void createEnemyMobileUnit( GameUnitType type, int posX, int posY, int enemyID )
    {
        if( null == enemies.get( enemyID ) ) {
            throw new IllegalStateException( "Synchronization failed" );
        }
        GameUnit unit = gameUnitFactory.createGameUnit( type, Color.RED  );
        unit.setX( posX );
        unit.setY( posY );
        enemies.get( enemyID ).addMobileUnit( unit );
    }

    @Override
    public void createEnemyPlayer( Player player )
    {
        this.enemies.add( player );
        super.createEnemyPlayer( player );
    }

}